//
//  CFCalendarView.m
//  CFfuturestalent
//
//  Created by 子yu 刘 on 2018/4/26.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "CFCalendarView.h"
#import "CFCalendarCell.h"

@interface CFCalendarView ()

@property (nonatomic ,strong) NSArray * weekArr;
@property (nonatomic ,strong) NSMutableArray * weekDateArr;
@property (nonatomic ,strong) UITableView * tableView;

@end

@implementation CFCalendarView

+(CFCalendarView *)sharedInstance{
    static CFCalendarView * calendarView;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        calendarView = [[CFCalendarView alloc] init];
    
    });
    return calendarView;
}

-(instancetype)init{
    if (self = [super init]) {
        [self initView];
    }
    return self;
}

+ (void)showCalenderViewOnView:(UIViewController *)target selectedData:(selectedDateBlock)block{
    [[CFCalendarView sharedInstance] initViewWithOnView:target selectedData:block];
}

- (void)initViewWithOnView:(UIViewController *)target selectedData:(selectedDateBlock)block {
    _block = block;
    self.frame = target.view.bounds;
    [target.view addSubview:self];
}


- (void)initView{
    _seletedIndex = [DateUtil getNowDateIndexDayInWeek:[NSDate date]]-2;
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    UIView * view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.width.mas_offset(130);
        make.height.mas_offset(249);
    }];
    
    UITableView * tableView = [[UITableView alloc] init];
    self.tableView = tableView;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.scrollEnabled = NO;
    tableView.delegate = self;
    tableView.dataSource = self;
    [view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellId = @"CFCalendarCell";
    CFCalendarCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[CFCalendarCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    cell.date = self.weekDateArr[indexPath.row];
    if (indexPath.row == _seletedIndex) {
        cell.isSelected = YES;
    }else{
        cell.isSelected = NO;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.seletedIndex = indexPath.row;
    
    [self.tableView reloadData];
    if (self.block) {
        self.block(self.weekDateArr[indexPath.row]);
    }
    [self remove];
}

-(void)remove{
    [self removeFromSuperview];
}


-(NSArray *)weekArr{
    if (!_weekArr) {
        _weekArr = @[@"星期一",@"星期二",@"星期三",@"星期四",@"星期五"];
    }
    return _weekArr;
}

- (NSMutableArray *)weekDateArr{
    if (!_weekDateArr) {
        _weekDateArr = [NSMutableArray arrayWithCapacity:0];
         NSDate * Monday = [DateUtil getFirstDateOfWeek:[NSDate date]];
        for (NSInteger i = 1; i < 6; i ++) {
            //从现在开始的24小时
            NSTimeInterval secondsPerDay = i * 24*60*60;
            NSDate *curDate = [NSDate dateWithTimeInterval:secondsPerDay sinceDate:Monday];
//            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//            [dateFormatter setDateFormat:@"YYYY年M月d日"];
//            NSString *dateStr = [dateFormatter stringFromDate:curDate];//几月几号
            [_weekDateArr addObject:curDate];
        }
    }
    return _weekDateArr;
}
@end
